//controlls
define(function (require, exports, module) {
    const Vue=require("vue");
    var template=require('text!./index.html');
    const api=require("api");
    const utils=require('utils');
    const weui=require('weui');
    module.exports=Vue.extend({
        template:template,
        data:function () {
            return {
                mobile_phone:'',
                verification_code:'',
                invite_code:'',
                agree:true,
                user_get_code:{},
                handling:false,
                timeout:0,
                timerId:0
            }
        },
        computed:{
            oauth:function () {
                return this.$store.getters.getOAuth;
            },
            isBack:function () {
                return this.$store.getters.getIsBack;
            }
        },
        beforeRouteEnter:function (to,from,next) {
            next(function (vm) {
                // if(!vm.oauth.open_id||vm.oauth.open_id==="undefined"){
                //     //window.location.href=utils.oauthUrl();
                // }
            })
        },
        created:function () {
        },
        methods:{
            getCode:function () {
                var v=this;
                if(v.handling||v.timeout>0||v.mobile_phone.length==0)return;
                if(!utils.validatePhone(v.mobile_phone)){
                    weui.topTips('请输入正确的手机号码');
                    return;
                }
                var loading = weui.loading('正在发送……');
                v.handling=true;
                api.getCode(v.mobile_phone,v.oauth.open_id).then(function (data) {
                    if(data.user_get_code&&data.user_get_code.return_code==="SUCCESS"){
                        v.user_get_code=data.user_get_code;
                        //设置验证码 重新获取时间
                        v.timeout=60;
                        v.timerSet();
                        weui.toast("验证码已发送！",1000);
                    }else {
                        weui.topTips(data.user_get_code.return_msg);
                    }
                }).catch(function (e) {
                    weui.topTips('网络异常');
                }).finally(function () {
                    v.handling=false;
                    loading.hide();
                });
            },
            submit:function () {
                var v=this;
                if(v.handling)return;
                if(!utils.validatePhone(v.mobile_phone)){
                    weui.topTips("请输入正确的手机号码！");
                    return;
                }
                if(v.verification_code.length==0){
                    weui.topTips("请输入验证码！");
                    return;
                }
                if(!v.agree){
                    weui.topTips("请先阅读并同意用车注册协议！");
                    return;
                }
                if(v.user_get_code.return_code!=="SUCCESS"){
                    weui.topTips("请先获取验证码！");
                    return;
                }
                if(v.user_get_code.mobile_phone!==v.mobile_phone){
                    weui.topTips("验证码错误！");
                    return;
                }
                var loading = weui.loading('正在提交……');
                v.handling=true;
                api.login(v.verification_code,v.invite_code,v.user_get_code).then(function(data){
                    const user_login = data.user_login;
                    if(user_login&&user_login.return_code==="SUCCESS"){
                        v.mobile_phone="";
                        v.verification_code="";
                        v.user_get_code={};
                        v.invite_code="";
                        v.$store.dispatch('UserInfo', user_login).then(function(data){
                            weui.toast("登录成功！",{duration:1000,callback:function () {
                                v.$router.push({ path: '/index' });
                                // //实名认证判断
                                // if(data.user_certification==0){
                                //     v.$router.push({ path: '/name' });
                                // }else if(data.deposit==0){//已交押金判断
                                //     v.$router.push({ path: '/deposit' });
                                // }else{
                                //     v.$router.push({ path: '/index' });
                                // }
                            }});
                        }).catch(function (error) {
                            weui.topTips(error||'网络异常！');
                        });
                    }else{
                        weui.topTips(user_login.return_msg);
                    }
                }).catch(function (error) {
                    weui.topTips('网络异常！');
                }).finally(function () {
                    v.handling=false;
                    loading.hide();
                });
            },
            agreeClick:function () {
                if(this.agree){
                    this.agree=false;
                }else{
                    this.agree=true;
                }
            },
            timerSet:function(){
                var v=this;
                if(v.timerId){clearInterval(v.timerId);}
                v.timerId=setInterval(function () {
                    if(v.timeout<=0){
                        clearInterval(v.timerId);
                    }else {
                        v.timeout--;
                    }
                },1000);
            }
        },
        filters:{
            timeText:function (val) {
                if(val===0)return '获取验证码';
                return '('+val+'s)重新获取';
            }
        }
    });
});